Part One: The Scene Graph

The red arrows represent eventIns routed to the Java script. The blue arrows represent fields which the Java script is aware of. Black arrows represent normal scene graph hierarchy structure.


Part Two: The VRML Source Code (flash.wrl)

Here is the entire VRML 2.0 source code. Type (or paste) this code into a file called "flash.wrl".

#VRML V2.0 utf8
 
DEF trBALL Transform {
  children [
    DEF tsBALL TouchSensor {}
    DEF shBALL Shape {
      appearance Appearance {
        material Material {
          diffuseColor 1 0 0
          specularColor 0.9 0.9 0.9
        }
      }
      geometry Sphere {
        radius 5
      }
    }
  ]
}
 
DirectionalLight {
  color 1 1 1
  direction -1 -1 -5
  intensity 0.3
}
 
DEF scrBALL Script {
  url "flash.class"
  field SFNode shBALL USE shBALL
  eventIn SFBool clicked
}
 
ROUTE tsBALL.isActive TO scrBALL.clicked

Part Three: The Java Source Code (flash.java)

Here is the Java source code. Type this into a file called "flash.java", then compile it to "flash.class".

import vrml.*;
import vs.*;

public class flash extends Script {
  SFNode nBALL = (SFNode)getField("shBALL");
  Shape shBALL = (Shape)nBALL.getValue();
  float coRED[] = new float[3];
  float coBLUE[] = new float[3];
  boolean isRed = true;

  public flash() {
    coRED[0] = 1.0f;
    coRED[1] = 0.0f;
    coRED[2] = 0.0f;
    coBLUE[0] = 0.0f;
    coBLUE[1] = 0.0f;
    coBLUE[2] = 1.0f;
  }

  public void clicked(ConstSFBool ev, ConstSFTime time) {
    if (ev.getValue() == true) {
      if (isRed == true) {
        shBALL.setColor(Shape.diffuse, coBLUE);
      } else {
        shBALL.setColor(Shape.diffuse, coRED);
      }
      isRed = !isRed;
    }
  }
}